package com.guhappy.stockutil;

// Generated Jul 19, 2011 6:12:32 PM by Hibernate Tools 3.4.0.CR1

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

/**
 * Home object for domain model class StockPriceHistrs.
 * @see  bd
 * @author  B.D
 */
public class StockPriceHistrHome {

	private static final Logger log = Logger.getLogger(StockPriceHistrHome.class);

	private static SessionFactory sessionFactory = getSessionFactory();

	protected static SessionFactory getSessionFactory() {
		sessionFactory = new Configuration().configure().buildSessionFactory();
		return sessionFactory;
	}

	public static void persist(StockPriceHistr transientInstance) {
		log.debug("persisting StockPriceHistrs instance");
		try {
			Session sess = sessionFactory.getCurrentSession();
			Transaction tran = sess.beginTransaction();
			sess.saveOrUpdate(transientInstance);
			tran.commit();
			/*sessionFactory.getCurrentSession().persist(transientInstance);*/
			log.debug("persist successful");
		} catch (StaleObjectStateException staleEx) {
			log.error("This interceptor does not implement optimistic concurrency control!");
			log.error("Your application will not work until you add compensation actions!");
            // Rollback, close everything, possibly compensate for any permanent changes
            // during the conversation, and finally restart business conversation. Maybe
            // give the user of the application a chance to merge some of his work with
            // fresh data... what you do here depends on your applications design.
            throw staleEx;
        } catch (Throwable ex) {
            // Rollback only
            ex.printStackTrace();
            try {
                if (sessionFactory.getCurrentSession().getTransaction().isActive()) {
                	log.debug("Trying to rollback database transaction after exception");
                	sessionFactory.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable rbEx) {
                log.error("Could not rollback transaction after exception!", rbEx);
            }
        }
	}

	public static void attachDirty(StockPriceHistr instance) {
		log.debug("attaching dirty StockPriceHistrs instance");
		try {
			Session sess = sessionFactory.getCurrentSession();
			Transaction tran = sess.beginTransaction();
			sess.saveOrUpdate(instance);
			tran.commit();
			/*sessionFactory.getCurrentSession().saveOrUpdate(instance);*/
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(StockPriceHistr instance) {
		log.debug("attaching clean StockPriceHistrs instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(StockPriceHistr persistentInstance) {
		log.debug("deleting StockPriceHistrs instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static StockPriceHistr merge(StockPriceHistr detachedInstance) {
		log.debug("merging StockPriceHistrs instance");
		try {
			StockPriceHistr result = (StockPriceHistr) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	
	public static StockPriceHistr findByTicker(String ticker){
		StockPriceHistr stock = null;
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		stock = (StockPriceHistr) session.createCriteria(StockPriceHistr.class).add(Restrictions.eq("sticker", ticker.trim())).uniqueResult();
		return stock;
	}
	
}
